﻿//变量类型  变量名= 变量赋值，变量类型有值类型：int=整型，float =浮点类型 ，double =双精度浮点类型 ，char 字符类型 ，类型 描述 
//bool 布尔类型, 值为true 或 false ，byte，decimal，enum，long，sbyte，short，struct，uint，ulong，ushort
//引用类型：class，delegate，dynamic，interface，object，string。内插字符串：void，var。

//访问修饰符                可访问性级别
//public	                    访问不受限制。
//protected	                访问限于包含类或派生自包含类的类型。
//internal	                    访问限于当前程序集。
//protected internal	    访问限于当前程序集或派生自包含类的类型。
//private	                    访问限于包含类。
//private protected	    访问限于包含类或当前程序集中派生自包含类的类型。 自 C# 7.2 之后可用。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Linq;
using 通用类库.Utils;
using 通用类库.DataProvider;
using 通用类库.Stock;

namespace CC股票手工类库                                 //CC股票手工类库是类名称, 这个类名包含在namespace 的命名空间中
{
    public partial class 股票手工 : UserControl       //股票手工是子窗体         是[股票手工] 下单界面
    {
        public 股票手工()
        {
            InitializeComponent();
            GlobalInstanceCache.Instance.DataInstance.OnStockPriceChanged += DataInstance_OnStockPriceChanged;
        }

        private void DataInstance_OnStockPriceChanged(object sender, StockPriceChangeEventArgs e)
        {
            if (e.Level2Info != null)
            {
                SetLevel2Price(e.Level2Info);
            }
        }

        public delegate void delegateWriteLevel2Price(StockLevel2Info pInfo);

        DateTime dtLast = DateTime.Now;
        private static object synchSetPRice = new object();
        internal void SetLevel2Price(StockLevel2Info pInfo)
        {
            lock (synchSetPRice)         //lock 关键字将语句块标记为关键部分，方法是获取给定对象的互斥锁，执行语句，然后释放该锁。 
        {
                if (String.Equals(this.股票代码1.Text, pInfo.StockCode, StringComparison.CurrentCultureIgnoreCase))
                {
                    if ((DateTime.Now - dtLast).TotalMilliseconds > 400)      //定时器
                    {
                        if (this.IsHandleCreated)
                        {
                            delegateWriteLevel2Price setUI = new delegateWriteLevel2Price(SetLevel2PriceUIValue);     // new关键字，是构造方法
                             this.Invoke(setUI, pInfo);
                        }

                    }
                }
            }
        }

        private static object synchUpdateUI = new object();
        private void SetLevel2PriceUIValue(StockLevel2Info pInfo)
        {
            lock (synchUpdateUI)         //lock 关键字将语句块标记为关键部分，方法是获取给定对象的互斥锁，执行语句，然后释放该锁。 
        {
                dtLast = DateTime.Now;
                ClearValue();
                int maxAmount = 0;
                if (pInfo.Buy1Amount != 0 && pInfo.Buy1Price != 0)
                {
                    this.买一量.Text = pInfo.Buy1Amount.ToString();
                    this.买二量.Text = pInfo.Buy2Amount.ToString();
                    this.买三量.Text = pInfo.Buy3Amount.ToString();
                    this.买四量.Text = pInfo.Buy4Amount.ToString();
                    this.买五量.Text = pInfo.Buy5Amount.ToString();

                    this.买一价.Text = pInfo.Buy1Price.ToString("##.00");
                    this.买二价.Text = pInfo.Buy2Price.ToString("##.00");
                    this.买三价.Text = pInfo.Buy3Price.ToString("##.00");
                    this.买四价.Text = pInfo.Buy4Price.ToString("##.00");
                    this.买五价.Text = pInfo.Buy5Price.ToString("##.00");

                    this.卖一量.Text = pInfo.Sale1Amount.ToString();
                    this.卖二量.Text = pInfo.Sale2Amount.ToString();
                    this.卖三量.Text = pInfo.Sale3Amount.ToString();
                    this.卖四量.Text = pInfo.Sale4Amount.ToString();
                    this.卖五量.Text = pInfo.Sale5Amount.ToString();

                    this.卖一价.Text = pInfo.Sale1Price.ToString("##.00");
                    this.卖二价.Text = pInfo.Sale2Price.ToString("##.00");
                    this.卖三价.Text = pInfo.Sale3Price.ToString("##.00");
                    this.卖四价.Text = pInfo.Sale4Price.ToString("##.00");
                    this.卖五价.Text = pInfo.Sale5Price.ToString("##.00");

                    this.涨停价.Text = pInfo.TopPrice.ToString("##.00");
                    this.跌停价.Text = pInfo.BottomPrice.ToString("##.00");
                    this.最新价格.Text = pInfo.Price.ToString("##.00");

        //计算和显示今日涨幅
                    if (pInfo.Price > pInfo.PreClosePrice)
                        this.涨幅比例.ForeColor = Color.Red;          //涨幅大于0显示红色
                    else
                        this.涨幅比例.ForeColor = Color.Green;      //涨幅小于0显示绿色
                    this.涨幅比例.Text = ((pInfo.Price - pInfo.PreClosePrice) * 100 / pInfo.PreClosePrice).ToString("##.00") + "%";   //今日涨幅计算     

        if (pInfo.Buy1Amount > maxAmount)
                        maxAmount = pInfo.Buy1Amount;
                    if (pInfo.Buy2Amount > maxAmount)
                        maxAmount = pInfo.Buy2Amount;
                    if (pInfo.Buy3Amount > maxAmount)
                        maxAmount = pInfo.Buy3Amount;
                    if (pInfo.Buy4Amount > maxAmount)
                        maxAmount = pInfo.Buy4Amount;
                    if (pInfo.Buy5Amount > maxAmount)
                        maxAmount = pInfo.Buy5Amount;

                    if (pInfo.Sale1Amount > maxAmount)
                        maxAmount = pInfo.Sale1Amount;
                    if (pInfo.Sale2Amount > maxAmount)
                        maxAmount = pInfo.Sale2Amount;
                    if (pInfo.Sale3Amount > maxAmount)
                        maxAmount = pInfo.Sale3Amount;
                    if (pInfo.Sale4Amount > maxAmount)
                        maxAmount = pInfo.Sale4Amount;
                    if (pInfo.Sale5Amount > maxAmount)
                        maxAmount = pInfo.Sale5Amount;

                    买一量柱.Maximum = maxAmount;
                    买二量柱.Maximum = maxAmount;
                    买三量柱.Maximum = maxAmount;
                    买四量柱.Maximum = maxAmount;
                    买五量柱.Maximum = maxAmount;

                    卖一量柱.Maximum = maxAmount;
                    卖二量柱.Maximum = maxAmount;
                    卖三量柱.Maximum = maxAmount;
                    卖四量柱.Maximum = maxAmount;
                    卖五量柱.Maximum = maxAmount;

                    this.买一量柱.Value = pInfo.Buy1Amount;
                    this.买二量柱.Value = pInfo.Buy2Amount;
                    this.买三量柱.Value = pInfo.Buy3Amount;
                    this.买四量柱.Value = pInfo.Buy4Amount;
                    this.买五量柱.Value = pInfo.Buy5Amount;

                    this.卖一量柱.Value = pInfo.Sale1Amount;
                    this.卖二量柱.Value = pInfo.Sale2Amount;
                    this.卖三量柱.Value = pInfo.Sale3Amount;
                    this.卖四量柱.Value = pInfo.Sale4Amount;
                    this.卖五量柱.Value = pInfo.Sale5Amount;

        //计算和显示今日涨幅
                   if (pInfo.Price > pInfo.PreClosePrice)
                   this.今日涨幅.ForeColor = Color.Red;          //涨幅大于0显示红色
                   else
                   this.今日涨幅.ForeColor = Color.Green;      //涨幅小于0显示绿色
                   this.今日涨幅.Text = ((pInfo.Price - pInfo.PreClosePrice)  / pInfo.PreClosePrice * 100).ToString("##.00") + "%";   //涨幅计算

        //计算和显示昨日涨幅

        //计算和显示本周涨幅

        //计算和显示前1周涨幅

        //计算和显示前2周涨幅

        //计算和显示前3周涨幅

        //计算和显示本月涨幅

        //计算和显示上月涨幅

        //计算和显示本季涨幅

        //计算和显示上季涨幅

        //计算和显示今年涨幅

        //计算和显示去年涨幅

        //计算和显示日MACD的值

        //计算和显示周MACD的值

        //计算和显示月MACD的值

        //计算和显示季MACD的值
        }
        }
        }

        //在标签内显示
        private void ClearValue()
        {
            this.买一量.Text = "---";
            this.买二量.Text = "---";
            this.买三量.Text = "---";
            this.买四量.Text = "---";
            this.买五量.Text = "---";

            this.买一价.Text = "---";
            this.买二价.Text = "---";
            this.买三价.Text = "---";
            this.买四价.Text = "---";
            this.买五价.Text = "---";

            this.卖一量.Text = "---";
            this.卖二量.Text = "---";
            this.卖三量.Text = "---";
            this.卖四量.Text = "---";
            this.卖五量.Text = "---";

            this.卖一价.Text = "---";
            this.卖二价.Text = "---";
            this.卖三价.Text = "---";
            this.卖四价.Text = "---";
            this.卖五价.Text = "---";

            this.涨停价.Text = "---";
            this.跌停价.Text = "---";
            this.最新价格.Text = "---";
            this.涨幅比例.Text = "---";

            this.买一量柱.Value = 0;
            this.买二量柱.Value = 0;
            this.买三量柱.Value = 0;
            this.买四量柱.Value = 0;
            this.买五量柱.Value = 0;

            this.卖一量柱.Value = 0;
            this.卖二量柱.Value = 0;
            this.卖三量柱.Value = 0;
            this.卖四量柱.Value = 0;
            this.卖五量柱.Value = 0;
        }

        //要求股票代码，股票名称全局同步，
        // 输入完股票代码立即在股票名称文本框内显示对应的股票名称
        private void 股票代码1_TextChanged(object sender, EventArgs e)                                             //股票代码1
        {
            string strTmp = StockCache.Instance.GetStockName(this.股票代码1.Text);                            //在代码文本框内输入股票代码1
            this.股票名称1.Text = strTmp;
            GlobalInstanceCache.Instance.DataInstance.SubscribeStock(this.股票代码1.Text);
        }

        //要求股票代码，股票名称全局同步，
        // 输入完股票代码立即在股票名称文本框内显示对应的股票名称
        private void 股票代码2_TextChanged(object sender, EventArgs e)                                             //股票代码2
        {
            string strTmp = StockCache.Instance.GetStockName(this.股票代码2.Text);                            //在代码文本框内输入股票代码2
            this.股票名称2.Text = strTmp;
            GlobalInstanceCache.Instance.DataInstance.SubscribeStock(this.股票代码2.Text);
        }

        //
        public void SetStockCode(string text)
        {
            this.股票代码1.Text = text;
            this.股票代码2.Text = text;
            string strTmp = StockCache.Instance.GetStockName(this.股票代码2.Text);
            this.股票名称1.Text = strTmp;
            this.股票名称2.Text = strTmp;
            StockLevel2Info PriceInfo = StockCache.Instance.GetStockPrice(this.股票代码1.Text);
            SetLevel2PriceUIValue(PriceInfo);
            股票代码1_TextChanged(null, null);
            股票代码2_TextChanged(null, null);
        }

        //显示财务信息

    }
}





